package cglgu.trellis.chado.das2.modelimpl;

/*
 * $Id: ChadoTypesCapability.java 191 2011-03-27 00:38:22Z sundaram.georgetown@gmail.com $
 * $Author: sundaram.georgetown@gmail.com $
 * $Date: 2011-03-27 00:38:22 +0000 (Sun, 27 Mar 2011) $
 * $Rev: 191 $
 * $HeadURL: http://genomancer.googlecode.com/svn/branches/gu-dev-branch/tengcha/src/cglgu/trellis/chado/das2/modelimpl/ChadoTypesCapability.java $
 */

import genomancer.trellis.das2.model.Das2CoordinatesI;
import genomancer.trellis.das2.model.Das2FormatI;
import genomancer.trellis.das2.model.Das2SegmentI;
import genomancer.trellis.das2.model.Das2SegmentsResponseI;
import genomancer.trellis.das2.model.Das2TypeI;
import genomancer.trellis.das2.model.Das2TypesCapabilityI;
import genomancer.trellis.das2.model.Das2TypesResponseI;
import genomancer.vine.das2.client.modelimpl.Das2Coordinates;
import genomancer.vine.das2.client.modelimpl.Das2GenericCapability;
import genomancer.vine.das2.client.modelimpl.Das2Type;
import genomancer.vine.das2.client.modelimpl.Das2TypesResponse;
import genomancer.vine.das2.client.modelimpl.Das2Version;
import cglgu.trellis.chado.das2.cglgu.Analysis;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class ChadoTypesCapability extends Das2GenericCapability implements Das2TypesCapabilityI  {
	boolean DEBUG = true;
	Das2TypesResponseI types_response = null;
	ChadoVersion chado_version = null;
	/** for convenience, uri2type maps both absolute URI and local URI to type */
	Map<URI,Das2TypeI> uri2type = null;


	private CglguDbUtil dbutil;

	// child types 
	protected Das2Type type_UTR;
	protected Das2Type type_CDS;
	protected Das2Type type_UNKNOWN;
	protected Das2Type type_START_CODON;
	protected Das2Type type_STOP_CODON;
	protected Das2Type type_POLYPEPTIDE
	protected Das2Type type_EXON;		

	boolean initialized = false;

	public ChadoTypesCapability(ChadoVersion version, Das2CoordinatesI coords, CglguDbUtil dbutil) {
		super(version.getBaseURI(), (version.getLocalURIString()+"/types"), "types", version, coords);
		chado_version = version;
		this.dbutil = dbutil;
	}

	public Das2TypeI getType(URI type_uri)  {
		if (! initialized)  {
			initTypes();
		}
		return uri2type.get(type_uri);
	}

	public Das2TypesResponseI getTypes() {
		if (! initialized)  {
			initTypes();
		}
		return types_response;
	}

	public Das2FormatI getFormat(String format_name) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	protected boolean initTypes()  {
		boolean success = false;
		uri2type = new HashMap<URI,Das2TypeI>();
		List<Das2TypeI> types = new ArrayList<Das2TypeI>();
		//	Set<String> table_names = new HashSet<String>();
		//	Set<String> merged_split_types = new HashSet<String>(); // types with ("all_" + type_name) table
		// map of track type names that have split tables to exemplar of table name
		Map<String, String> possible_split_types = new HashMap<String, String>();
		Map<String, String> group_name2label = new HashMap<String, String>();

		/** mapping of tableName field in trackDb (which is also used for type name)
		 *     to actual table name in database (or null for split fields...) 
		 */
		Map<String, String> type2table = new HashMap<String, String>();
		List<Das2TypeI> extra_types = new ArrayList();

		URI type_base_uri = this.getAbsoluteURI().resolve("./");
		type_UTR = new Das2Type(type_base_uri, "UTR", "UTR", null, null, "UTR", null, false);
		type_CDS = new Das2Type(type_base_uri, "CDS", "CDS", null, null, "CDS", null, false);
		type_START_CODON = new Das2Type(type_base_uri, "START_CODON", "START_CODON", null, null, "start_codon", null, false);
		type_STOP_CODON = new Das2Type(type_base_uri, "STOP_CODON", "STOP_CODON", null, null, "stop_codon", null, false);
		type_POLYPEPTIDE = new Das2Type(type_base_uri, "POLYPEPTIDE", "POLYPEPTIDE", null, null, "polypeptide", null, false);
		type_EXON = new Das2Type(type_base_uri, "EXON", "EXON", null, null, "exon", null, false);		
		type_UNKNOWN = new Das2Type(type_base_uri, "unknown", "unknown", null, null, "unknown", null, false);

		// Here are the different feature types that are
		// currently loaded in the wasp feature table
		extra_types.add(type_UTR); 
		extra_types.add(type_CDS);
		extra_types.add(type_START_CODON);
		extra_types.add(type_STOP_CODON);
		extra_types.add(type_POLYPEPTIDE);
		extra_types.add(type_EXON);
		extra_types.add(type_UNKNOWN);

		for (Das2TypeI utype : extra_types)  {
			uri2type.put(utype.getAbsoluteURI(), utype);
			uri2type.put(utype.getLocalURI(), utype);
		}

		List<Analysis> analysisList = dbutil.getAnalysisList();

		if (analysisList == null){
			System.err.println("analysisList is null");
			return false;
		}

		int analysisCtr = 0;

		for (Analysis analysis :  analysisList){
			analysisCtr++;
			ChadoType chadoType = new ChadoType(type_base_uri, chado_version, analysis);
			types.add(chadoType);		
		}

		if (analysisCtr == 0){
			System.err.println("number of analyses was " + analysisCtr);
			return false;
		}

		types_response = new Das2TypesResponse(type_base_uri, types, null);

		for (Das2TypeI utype : types)  {
			uri2type.put(utype.getAbsoluteURI(), utype);
			uri2type.put(utype.getLocalURI(), utype);
		}

		success = true;
		initialized = true;
	}

	return success;
}